import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {
    state = {
        mouse: false
    }
    render() {
        const { id, name, done,deleteTodo} = this.props
        return (
            <li onMouseLeave={this.handleMouse(false)} onMouseEnter={this.handleMouse(true)}
            style={{backgroundColor: this.state.mouse ? '#ddd': '#fff'}}>
                <label>
                    {/* defaultChecked 只在第一次起作用 */}
                    <input type="checkbox" checked={done} onChange={this.onChange(id)} />
                    {name}
                </label>
                <button onClick={()=>{deleteTodo(id)}} className="btn btn-danger" style={{display: this.state.mouse ? 'block': 'none'}}>删除</button>
            </li>
        )
    }
    onChange = (id) => {
        const {updateTodo} = this.props
        return ({target:{checked}})=>{
            updateTodo(id,checked)
        }
    }
    handleMouse = (flag) => {
        return () => {
            this.setState({mouse:flag})
        }
    }
}
 